package com.example.tatil.service.impl;

import com.example.tatil.mapper.EmpMapper;
import com.example.tatil.pojo.Emp;
import com.example.tatil.pojo.PageBean;
import com.example.tatil.service.EmpService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class EmpServicelmpl implements EmpService {

    @Autowired
    private EmpMapper empMapper;


//    @Override
//    public void deleter(List<Integer> ids) {
//
//    }

//    @Override
//    public PageBean page(Integer page, Integer pageSize) {
//
//        //1获取总记录数
//
//        Long count = empMapper.count();
//        //2获取分页查询结果列表
//        //起始索引
//        Integer start = (page-1)*pageSize;
//        List<Emp> empList = empMapper.page(start, pageSize);
//        //3封装PageBean对象
//        PageBean pageBean = new PageBean(count,empList);
//        return pageBean;
//
//
//    }

    @Override
    public PageBean page(Integer page, Integer pageSize,String name, Short gender, LocalDate begin, LocalDate end) {
        //设置分页参数
        PageHelper.startPage(page,pageSize);
        //2执行查询
        List<Emp> emplist = empMapper.list(name,gender,begin,end);
        Page<Emp> p = (Page<Emp>) emplist;
        //3封装PageBean对象
        PageBean pageBean = new PageBean(p.getTotal(),p.getResult());
        return pageBean;


    }

    //批量删除员工
    @Override
    public void deleter(List<Integer> ids) {
        empMapper.deleter(ids);

    }

    //添加员工
    @Override
    public void add(Emp emp) {
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());

        empMapper.add(emp);
    }
    //根据id查询员工

    @Override
    public Emp select(Integer id) {
        Emp emp = empMapper.select(id);

        return emp;
    }

    @Override
    public void update(Emp emp) {
        //补齐基本数据
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.update(emp);
    }

    @Override
    public Emp login(Emp emp) {
        Emp byUsernameAndPassword = empMapper.getByUsernameAndPassword(emp);
        return byUsernameAndPassword;
    }
}
